package com.tecdam.payment.samples;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;
import javax.microedition.payment.TransactionListener;
import javax.microedition.payment.TransactionModule;
import javax.microedition.payment.TransactionModuleException;
import javax.microedition.payment.TransactionRecord;

/**
 * @author rafaelrocha
 */
public class MyGame extends MIDlet implements TransactionListener, CommandListener {

    private TransactionModule transModulo;
    private int aNivel = 0;
    private int nbNivel = 0;
    private int MAX_NIVEL = 3;
    private Display tela;
    private Command sair;

    public MyGame() {
        tela = Display.getDisplay(this);
        Form formMain = new Form("GamePay");
        sair = new Command("Exit", Command.EXIT, 1);
        formMain.addCommand(sair);
        formMain.setCommandListener(this);
        tela.setCurrent(formMain);

        try {
            transModulo = new TransactionModule(this);
        } catch(TransactionModuleException e) {
            e.toString();
        } catch(Exception e) { }
        try {
          transModulo.setListener(this);
        } catch(Exception e) { }
    }

    public void startApp() {
        while (nbNivel <= MAX_NIVEL) {
            // pagamento (payment) obrigatório p/ o nível
            try {
                transModulo.process(aNivel, "Prox Nível", "Libera o acesso ao nível seguinte.");
                synchronized(this) {
                    try {
                        wait(); // esperar até que a rechamada seja feita
                    } catch (InterruptedException ie) { }
                }
            } catch (Exception e) { }

            // início do nível
            Form formLevel = new Form("Nivel " + nbNivel);
            tela.setCurrent(formLevel);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.getMessage();
            }
            // fim do nível
            nbNivel++;
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void processed(TransactionRecord record){
        switch(record.getState()) {
            case TransactionRecord.TRANSACTION_SUCCESSFUL:
                // transação de pagamento bem sucedida
                break;
            case TransactionRecord.TRANSACTION_REJECTED:
                // transação de pagamento mal sucedida, deve-se pagar para jogar
                break;
            case TransactionRecord.TRANSACTION_FAILED:
            default:
                // problemas técnicos - tente de novo
                break;
        }

        synchronized(this) {
            notify(); // notifica emquanto espera thread
        }
    }

    public void commandAction(Command command, Displayable displayable) {
         if (command == sair) {
            destroyApp(true);
            notifyDestroyed();
        }
    }

   

}